﻿using NLog;

using PostSharp.Laos;

using System;
using System.Globalization;

namespace codeparser.net.v2.Aspects
{
    /// <summary>
    /// Represents the logging aspect.
    /// </summary>
    [Serializable]
    internal sealed class LoggingAspectAttribute : OnMethodBoundaryAspect
    {
        /// <summary>
        /// Called when a method is entered.
        /// </summary>
        /// <param name="eventArgs">The method execution event arguments.</param>
        public override void OnEntry(MethodExecutionEventArgs eventArgs)
        {
            Logger log = LogManager.GetLogger(eventArgs.Method.DeclaringType.FullName);
            log.Debug(CultureInfo.CurrentCulture, "Enter method '{0}'",
                eventArgs.Method.DeclaringType.FullName,
                eventArgs.Method.Name);
        }

        /// <summary>
        /// Called when a method is left.
        /// </summary>
        /// <param name="eventArgs">The method execution event arguments.</param>
        public override void OnSuccess(MethodExecutionEventArgs eventArgs)
        {
            Logger log = LogManager.GetLogger(eventArgs.Method.DeclaringType.FullName);
            log.Debug(CultureInfo.CurrentCulture, "Exit method '{0}'",
                eventArgs.Method.DeclaringType.FullName,
                eventArgs.Method.Name);
        }

        /// <summary>
        /// Called when an exception occurs within a method.
        /// </summary>
        /// <param name="eventArgs">The method execution event arguments.</param>
        public override void OnException(MethodExecutionEventArgs eventArgs)
        {
            Logger log = LogManager.GetLogger(eventArgs.Method.DeclaringType.FullName);
            log.Debug(CultureInfo.CurrentCulture, "Exception in method '{0}'",
                eventArgs.Method.DeclaringType.FullName,
                eventArgs.Method.Name);
        }
    }
}